#!/bin/bash

WAYLAND_SCANNER="wayland-scanner"
WL_PROTOCOL_DIR="/usr/share/wayland-protocols"
OUTPUT_DIR=$(dirname "\$0")  # 脚本所在目录

# 切换到输出目录
cd "$OUTPUT_DIR" || exit 1

PROTOCOLS=(
	"$WL_PROTOCOL_DIR/unstable/xdg-output/xdg-output-unstable-v1.xml"
	"$WL_PROTOCOL_DIR/unstable/linux-dmabuf/linux-dmabuf-unstable-v1.xml"
	"$OUTPUT_DIR/core/core_wl/protocol/wlr-screencopy-unstable-v1.xml"
)


# 生成 .h 和 .c 文件
for proto in "${PROTOCOLS[@]}"; do
	base=$(basename "$proto" .xml)
	$WAYLAND_SCANNER client-header "$proto" "${base}-client-protocol.h"
	$WAYLAND_SCANNER private-code "$proto" "${base}-protocol.c"
done

# 编译静态库
gcc -c *-protocol.c $(pkg-config --cflags wayland-client)
ar rcs libwl_protos.a *.o

# 清理中间文件
rm -f *.o

mv *.h "$OUTPUT_DIR/core/core_wl/protocol"
mv *.c "$OUTPUT_DIR/core/core_wl/protocol"
mv *.a "$OUTPUT_DIR/core/core_wl/protocol"

